using System;
using System.Windows.Forms;
using System.ComponentModel;

namespace Woz
{
    class AppContext : ApplicationContext
    {
        public AppContext()
        {
            // initialize the splash form
            this.mSplashForm = new SplashForm(false);

            // initialize the main form
            Woz.MainForm.Instance.FormClosing += this.OnMainFormClosing;

            // show the splash form
            this.mSplashForm.Show();

            // tell the resource manager to initialize itself
            this.mSplashForm.StatusLabelText = "Initializing Resources...";
            this.mSplashForm.Refresh();
            if (!Woz.ResourceManager.Instance.Initialize())
            {
                throw new Exception("Error: ResourceManager failed to initialize");
            }


            // tell the interface manager to initialize
            this.mSplashForm.StatusLabelText = "Creating User Interfaces Objects...";
            this.mSplashForm.Refresh();
            if (!Woz.InterfaceManager.Instance.Initialize())
            {
                throw new Exception("Error: InterfaceManager failed to initialize");
            }

            // tell the main form to start building its interface
            this.mSplashForm.StatusLabelText = "Building User Interface...";
            this.mSplashForm.Refresh();
            Woz.MainForm.Instance.BuildUserInterface();

            // tell the main form to create its modes
            this.mSplashForm.StatusLabelText = "Finalizing...";
            this.mSplashForm.Refresh();
            Woz.MainForm.Instance.FinalizeInitialization();

            // hide the splash form
            this.mSplashForm.Close();
            
            // kill the splash form to free up some memory since were never going to use it again
            this.mSplashForm.Dispose();

            // set the main form in the appcontext
            this.MainForm = Woz.MainForm.Instance;

            // show the main form
            Woz.MainForm.Instance.Show();

            // load the first file specified in the command line
            string[] files = Environment.GetCommandLineArgs();
            if (files.Length > 1)
            {
                Woz.MainForm.Instance.LoadProject(files[1]);
            }
        }

        private void OnMainFormClosing(object pSender, FormClosingEventArgs pEventArgs)
        {
            // show the confirmation box
//            DialogResult result = MessageBox.Show("Are sure you'd like to exit?", "Woz", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);

            // check if they really want to quit
//            if (result == DialogResult.No)
//            {
                // they actually dont want to quit so cancel this closing
//                pEventArgs.Cancel = true;
//            }
        }

        private Woz.SplashForm mSplashForm;
    }
}